<template>
    <el-dialog title="选择商品" :visible.sync="dialogVisible" :close-on-click-modal="false">
        <div class="content" @click.stop="()=>{}">
            <el-form label-position="left" label-width="100px" style="width: 100%; padding-left: 20px;">
                <el-form-item label="所选商品：" v-if="config.count !== 1">
                    <div class="el-tag-box">
                        <el-tag v-for="(item, index) of result" :key="item.goodsId" :closable="true" @close="delItem(index)">{{item.title}}</el-tag>
                    </div>
                </el-form-item>
            </el-form>
            <el-form ref="searchForm" class="search-block" :model="listQuery" label-position="left" label-width="100px" style="width: 100%">
                <input type="text" style="display: none"/><!--form input placeholder-->
                <el-row>
                    <el-col>
                        <el-form-item label="商品名称：" prop="name">
                            <el-input @keyup.enter.native="search" v-model="listQuery.name" placeholder="按商品名称搜索" maxlength="50" clearable></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item label="" class="btn-item">
                    <el-button type="primary" size="mini" icon="el-icon-search" plain @click="search">搜索</el-button>
                </el-form-item>
            </el-form>
            <el-table :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border @row-click="clickRow">
                <el-table-column align="center" width="50">
                    <template slot-scope="scope">
                        <label class="input-radio" v-if="config.count === 1">
                            <input type="radio" :value="{goodsId: scope.row.goodsId}" v-model="itemsRadio" @change="(e) => setValue(e.target.checked, scope.row)"/>
                            <span></span>
                        </label>
                        <label class="input-checkbox" v-else>
                            <input type="checkbox" :value="{goodsId: scope.row.goodsId}" v-model="itemsCheck" @change="(e) => setValue(e.target.checked, scope.row)"/>
                            <span></span>
                        </label>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="商品名称" min-width="110" show-overflow-tooltip>
                    <template slot-scope="scope">
                        <div class="text-justify">
                            <p v-text="scope.row.goodsTitle"></p>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="虚拟商品" width="100" v-if="!config.goodsType">
                    <template slot-scope="scope">
                        <div class="text-justify">
                            <span v-if="scope.row.goodsType===2" style="color: red">是</span>
                            <span v-else>否</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="款号" width="110">
                    <template slot-scope="scope">
                        <span>{{scope.row.goodsSn}}</span>
                    </template>
                </el-table-column>
                <!-- <el-table-column align="center" label="分类" min-width="60" >
                    <template slot-scope="scope">
                        <span>{{scope.row.categoryName}}</span>
                    </template>
                </el-table-column> -->
            </el-table>
            <el-pagination
                    @current-change="handleCurrentChange"
                    :current-page.sync="listQuery.pageNo"
                    :page-size="listQuery.pageSize"
                    background
                    layout="total, prev, pager, next, jumper"
                    style="text-align: center"
                    :total="listTotal">
            </el-pagination>
        </div>
        <div slot="footer" class="dialog-footer" @click.stop="()=>{}">
            <el-button type="primary" :loading="btnLoading" @click="choose">确定</el-button>
            <el-button @click="close">取消</el-button>
        </div>
    </el-dialog>
</template>
<script>
    export default {
      name: 'activity-goods',
      props: {
        config: { type: Object, default(){ return {} } },
        selected: { type: Array, default(){ return [] } },
      },
      data(){
        return {
          dialogVisible: true,
          btnLoading: false,
          result: this.selected,
          itemsCheck: this.selected.length ? this.selected.map(item => { return { goodsId: item.goodsId }}): [],
          itemsRadio: this.selected.length ? {goodsId: this.selected[0].goodsId} : {},
          listQuery: {
            title: '',
            pageNo: 1,
            pageSize: 10,
            promotionId: this.config.promotionId
          },
          list: [],
          listTotal: 0,
          listLoading: false
        }
      },
      created(){
        this.getList()
        console.log(this.result)
      },
      methods: {
        // 获取列表
        getList() {
          this.listLoading = true;
          this.request({
            url: '/promotion/goods',
            method: 'get',
            params: this.listQuery
          }).then(({data}) => {
            this.list = data.records
            this.listTotal = data.total
            this.listLoading = false
          }).catch(err => {
            this.list = [];
            this.listTotal = 0;
            this.listLoading = false;
          });
        },
        // 搜索
        search() {
          this.listQuery.pageNo = 1;
          this.getList();
        },
        // 切换分页
        handleCurrentChange(page) {
          this.listQuery.pageNo = page;
          this.getList();
        },
        // 选择change
        setValue(checked, row){
          if(checked){     
            if(this.config.count ===1 ){
              this.result = [{ goodsId: row.goodsId,
              pic: row.majorPic,
              title: row.goodsTitle,
              subtitle: row.subName,
              lanPrice: row.lanPrice,
              price: row.price,
              type: 1,
              from_type: 1 }]
            } else {
              this.result.push({ goodsId: row.goodsId,
              pic: row.majorPic,
              title: row.goodsTitle,
              subtitle: row.subName,
              lanPrice: row.lanPrice,
              price: row.price,
              type: 1,
              from_type: 1 });
            }
          } else {
            let index = this.itemsCheck.findIndex(item => row.goodsId === item.goodsId);
            this.result.splice(index, 1);
          }
        },
        // 单击行
        clickRow(row) {
          if (this.config.count === 1) {
            this.itemsRadio = { goodsId: row.goodsId };
            this.result = [{ goodsId: row.goodsId,
              pic: row.majorPic,
              title: row.goodsTitle,
              subtitle: row.subName,
              lanPrice: row.lanPrice,
              price: row.price,
              type: 1,
              from_type: 1 }];
          } else {
            let index = this.itemsCheck.findIndex(item => row.goodsId === item.goodsId);
            if (index < 0){
              this.itemsCheck.push({ goodsId: row.goodsId });
              this.result.push({ goodsId: row.goodsId,
              pic: row.majorPic,
              title: row.goodsTitle,
              subtitle: row.subName,
              lanPrice: row.lanPrice,
              price: row.price,
              type: 1,
              from_type: 1 });
            } else {
              this.itemsCheck.splice(index, 1);
              this.result.splice(index, 1);
            }
          }
        },
        // 通过tag删除
        delItem(index) {
          this.itemsCheck.splice(index, 1);
          this.result.splice(index, 1);
        },
        // 关闭弹窗
        close(){
          this.dialogVisible = false;
          document.body.removeChild(this.$el);
        },
        // 选完，点击确定
        choose(){
          if(this.result.length < 1){
            return this.$message.error('请选择商品');
          }
          if(this.config.count > 0 && this.itemsCheck.length > this.config.count){
            return this.$message.error(`最多选择${this.config.count}个商品`);
          }
          this.$emit('choose', this.result);
          this.close();
        }
      }
    }
</script>
